Imports AjaxPro
Imports MIMS.WRS.Business
Imports MIMS.WRS.Entities
Imports System.Text
Imports System.Web

Public Class Constants
    Public Const QUOTE_SIGN As String = "~"
End Class

Public Class AjaxUtilMethod

    <AjaxPro.AjaxMethod()> _
    Public Function GetSupplierHTML(ByVal conPointID As Integer, ByVal selectedSupplier As String) As String
        Dim suppliers As SupplierCollection = SupplierService.GetSuppliers(conPointID)

        Dim sb As StringBuilder = New StringBuilder
        If suppliers.Count > 0 Then
            sb.Append("<select id='ddlSupplier'>")
            sb.Append("<option value=''>[--select supplier--]</option>")
            For Each supplier As supplier In suppliers
                If supplier.SupplierCode = selectedSupplier Then
                    sb.AppendFormat("<option selected value='{0}'>{1}</option>", supplier.SupplierCode, supplier.SupplierCode)
                Else
                    sb.AppendFormat("<option value='{0}'>{1}</option>", supplier.SupplierCode, supplier.SupplierCode)
                End If
            Next
            sb.Append("</select>")
        Else
            sb.Append("<i>no suppliers available</i>")
        End If


        Return sb.ToString()
    End Function

    <AjaxPro.AjaxMethod()> _
  Public Function GetSupplierHTMLForReportHeader(ByVal conPointID As Integer, ByVal selectedSupplier As String) As String
        Dim suppliers As SupplierCollection = SupplierService.GetSuppliers(conPointID)

        Dim sb As StringBuilder = New StringBuilder
        Dim suppCode As String = ""
        If suppliers.Count > 0 Then
            sb.Append("<select id='ddlSupplier' onchange='supplierChanged(this)'>")
            sb.Append("<option value=''>[All]</option>")
            For Each supplier As supplier In suppliers
                If supplier.SupplierCode = selectedSupplier Then
                    suppCode = selectedSupplier
                    sb.AppendFormat("<option selected value='{0}'>{1}</option>", supplier.SupplierCode, supplier.SupplierCode)
                Else
                    sb.AppendFormat("<option value='{0}'>{1}</option>", supplier.SupplierCode, supplier.SupplierCode)
                End If
            Next
            sb.Append("</select>")
        Else
            sb.Append("[no suppliers available]")
        End If

        'If (suppCode.Length > 0) Then
        '    sb.AppendFormat("<script>alert('{0}');hidSupplier.value = '{0}';</script>", suppCode)
        'End If
        Return sb.ToString()
    End Function

    <AjaxPro.AjaxMethod()> _
    Public Function GetResponsibleHTML(ByVal supplierCode As String, ByVal officeID As String) As String

        Dim supp As Supplier = SupplierService.GetSupplier(supplierCode)
        'Dim conPoint As conPoint = ConPointService.GetConPoint(supp.ConPointID)
        Dim responsibles As ResponsibleCollection = ResponsibleService.GetResponsiblesByConPoint(supp.ConPointID)

        Dim sb As StringBuilder = New StringBuilder
        If responsibles.Count > 0 Then
            sb.Append("<select name='ddlResponsible' id='ddlResponsible'>")
            sb.Append("<option value=''>[--select responsible--]</option>")
            For Each resp As Responsible In responsibles
                Dim respOfficeCollection As ResponsibleOfficeCollection = ResponsibleOfficeService.GetResponsibleOfficesByResponsible(resp.ResponsibleID)

                Dim found As Boolean = False
                For Each respOffice As ResponsibleOffice In respOfficeCollection
                    If (respOffice.DefaultToOfficeID = Convert.ToInt32(officeID)) Then
                        found = True
                        Exit For
                    End If
                Next

                If found Then
                    sb.AppendFormat("<option selected value='{0}'>{1}</option>", resp.ResponsibleID, resp.Name)
                Else
                    sb.AppendFormat("<option value='{0}'>{1}</option>", resp.ResponsibleID, resp.Name)
                End If
            Next
            sb.Append("</select>")
        Else
            sb.Append("<i>[no responsible available]</i>")
        End If
        Return sb.ToString()
    End Function

    <AjaxPro.AjaxMethod()> _
    Public Function AddToShoppingCart(ByVal shoppingItem As String, ByVal countryId As Integer, ByVal deliveryAndPricingID As Integer, ByVal territoryID As Integer, ByVal territoryName As String, ByVal divAddingCartId As String, ByVal countryName As String) As String
        Dim arrValues As String()
        Dim strArgument As String = HttpUtility.HtmlDecode(shoppingItem).Replace(Constants.QUOTE_SIGN, "'")
        arrValues = strArgument.Split("@"c)

        Dim result As String = AddItemToCard(arrValues, countryId, deliveryAndPricingID, territoryID, territoryName, countryName)
        Return result & "@" & divAddingCartId
    End Function

    Private Function AddItemToCard(ByVal arrValues As String(), ByVal countryId As Integer, ByVal deliveryAndPricingID As Integer, ByVal territoryID As Integer, ByVal territoryName As String, ByVal countryName As String) As String
        Dim oCollection As New Collection
        Dim bulkOrderId As Int32

        bulkOrderId = Convert.ToInt32(arrValues(6))
        Dim bSameTerritory As Boolean = True

        If (Not System.Web.HttpContext.Current.Session(Constant.SS_SHOPPING_LIST) Is Nothing) Then
            oCollection = DirectCast(System.Web.HttpContext.Current.Session(Constant.SS_SHOPPING_LIST), Collection)
            If (Not oCollection Is Nothing) Then
                For i As Integer = 1 To oCollection.Count
                    Dim selctedshoppingItem As ShoppingItem = DirectCast(oCollection(i), ShoppingItem)
                    If (selctedshoppingItem.BulkOrderId = bulkOrderId AndAlso bulkOrderId > 0) Then
                        Dim scriptString As String = "You have already selected this item."
                        Return scriptString
                    End If
                    If (deliveryAndPricingID = CType(DeliveryAndPricing.HD, Integer) And selctedshoppingItem.TerritoryID <> "" And selctedshoppingItem.TerritoryID <> CType(territoryID, String)) Then
                        bSameTerritory = False
                    End If

                    If (selctedshoppingItem.CountryId <> 0 AndAlso selctedshoppingItem.CountryId <> countryId) Then
                        Return "2" 'Remind user to select items from the same country
                    End If
                Next
            End If
        End If
        If (bSameTerritory = False) Then
            Dim scriptString As String = "The item is not the same territory with previous selected items. View the shopping list for selected details. Please select others."
            Return scriptString
        End If
        Dim item As New ShoppingItem
        With item
            ' .ItemNo = Convert.ToInt32(IIf(selectedIndex = 0, oCollection.Count + 1, selectedIndex))
            .ItemNo = Convert.ToInt32(arrValues(8))
            .ProductCode = arrValues(0).ToString
            .Description = arrValues(1).ToString
            .Quantity = 1
            .UnitDesc = arrValues(2).ToString
            Try
                .UnitSettingID = Convert.ToInt32(arrValues(3))
            Catch ex As Exception
            End Try
            Dim minOrderQtyValue As Integer = 1
            Try
                minOrderQtyValue = Integer.Parse(arrValues(9))
            Catch ex As Exception
            End Try
            If arrValues(4).ToString.Length > 0 Then
                .UnitPrice = Convert.ToDouble(arrValues(4))
                .Total = .Quantity * .UnitPrice * minOrderQtyValue
            End If
            .UnitWeight = Convert.ToDecimal(arrValues(5))
            .Priority = -1
            .BulkOrderId = Convert.ToInt32(arrValues(6))
            If (deliveryAndPricingID = CType(DeliveryAndPricing.HD, Integer)) Then
                .TerritoryID = territoryID.ToString()
            End If
            .MinQtyVolume = Convert.ToDecimal(arrValues(7))
            .CountryId = countryId
            .MinOrderQtyValue = Convert.ToInt32(arrValues(9))
        End With
        oCollection.Add(item)

        System.Web.HttpContext.Current.Session(Constant.SS_SHOPPING_LIST) = oCollection
        System.Web.HttpContext.Current.Session("FOBorHD") = IIf(deliveryAndPricingID = CType(DeliveryAndPricing.HD, Integer), "HD", "FOB")
        System.Web.HttpContext.Current.Session(Constant.SS_TERRITORY) = territoryName
        System.Web.HttpContext.Current.Session(Constant.SS_COUNTRY) = countryName
        System.Web.HttpContext.Current.Session(Constant.SS_CONPOINT_ID) = countryId
        System.Web.HttpContext.Current.Session(Constant.SS_SELECTED_CONPOINT_ID) = countryId
        If (oCollection.Count = 1 AndAlso ShoppingConext.ShopingUserId = 0) Then
            'Remind user to get saved list if any
            Return "1"
        End If

        Return ""
    End Function

    <AjaxPro.AjaxMethod()> _
   Public Function GetTerritoryHTML(ByVal countryID As Integer) As String
        Dim territories As TerritoryCollection = TerritoryService.GetTerritoriesForIDSByCountry(countryID)

        Dim sb As StringBuilder = New StringBuilder
        If territories.Count > 0 Then
            sb.Append("<select id='ddlTerritory'>")
            sb.Append("<option value=''>[--select--]</option>")
            For Each item As Territory In territories
                sb.AppendFormat("<option value='{0}'>{1}</option>", item.TerritoryID, item.Description)
            Next
            sb.Append("</select>")
        Else
            sb.Append("<i>no territories available</i>")
        End If

        Return sb.ToString()
    End Function
End Class
